<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['order:order:remove']">
          批量删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="shoppingcarList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="名称" align="center" prop="name" />
      <el-table-column label="学生编号" align="center" prop="studentId" />
      <el-table-column label="菜品编号" align="center" prop="dishId" />
      <el-table-column label="数量" align="center" prop="number" />
      <el-table-column label="金额" align="center" prop="amount" />
      <el-table-column label="图片" align="center" prop="picture">
        <template slot-scope="scope">
          <image-preview :src="scope.row.picture" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-shopping-bag-1"
            v-on:click="addOrder(scope.row.id)"
          >生成订单</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            class="weui-btn purchase-button weui-btn_mini weui-btn_primary"
            v-hasPermi="['shoppingcar:shoppingcar:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />


  </div>
</template>

<script>
import {
  listShoppingcar,
  delShoppingcar,
  addShoppingcar,
  updateShoppingcar,
} from "@/api/shoppingcar/shoppingcar";
import { addToOrder} from "@/api/order/order";

export default {
  name: "Shoppingcar",

  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 购物车表格数据
      shoppingcarList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        studentId: null,
        dishId: null,
        dishflavor: null,
        number: null,
        amount: null,
        picture: null,
        isDelete: null
      },
      // 表单参数
      form: { id : null},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询购物车列表 */
    getList() {
      this.loading = true;
      listShoppingcar(this.queryParams).then(response => {
        this.shoppingcarList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateShoppingcar(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addShoppingcar(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除购物车').then(function() {
        return delShoppingcar(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 结算订单 */
    addOrder(id){
      // 获取用户 ID
      const userId = localStorage.getItem("userId");
      if (!userId) {
        this.$modal.msgError("用户未登录，请先登录");
        return;
      }

      // 设置表单数据
      this.form.userId = userId;
      this.form.shopingId = id; // 使用传入的 shoppingId

      // 检查 shoppingId 是否有效
      if (!this.form.shopingId) {
        this.$modal.msgError("请选择有效的菜品");
        return;
      }

      // 调用添加到购物车方法
      addToOrder(this.form)
        .then(response => {
            this.$modal.msgSuccess("订单生成成功");
        })
    }
  }
};
</script>
